'''
1.0.0 版本 
# Date: 2024/12/28
# Author: ZHENG
# Description:
    # 此版本功能如下：
        1. 

'''

# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QTableView, QHeaderView, QComboBox, QLabel, QPushButton, QVBoxLayout, QWidget, QHBoxLayout, QPushButton, QAbstractItemView
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt
from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QApplication
import sys

class NavGoalTableView(QWidget):
    signalSendNavGoal = pyqtSignal(object)
    signalTaskFinish = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.table_view_ = QTableView()
        self.table_model_ = QStandardItemModel()
        self.table_view_.setModel(self.table_model_)
        self.table_model_.setHorizontalHeaderLabels(["点位名", "任务状态", "删除", "运行"])
        
        headerView = self.table_view_.horizontalHeader()
        headerView.setSectionResizeMode(QHeaderView.ResizeToContents)
        self.table_view_.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.table_view_.setSelectionMode(QAbstractItemView.SingleSelection)

        self.table_model_.itemChanged.connect(self.on_item_changed)

        # 新增点位按钮
        self.add_point_button = QPushButton("新增点位")
        self.add_point_button.clicked.connect(self.add_item)

        # 保存点位链按钮
        self.save_chain_button = QPushButton("保存点位链")
        self.save_chain_button.clicked.connect(self.save_point_chain)

        # 布局
        layout = QVBoxLayout()
        button_layout = QHBoxLayout()
        button_layout.addWidget(self.add_point_button)
        button_layout.addWidget(self.save_chain_button)
        layout.addLayout(button_layout)
        layout.addWidget(self.table_view_)
        self.setLayout(layout)

    @pyqtSlot('QStandardItem*')
    def on_item_changed(self, item):
        if item.column() == 0:
            print(f"点位名: {item.text()}")
        elif item.column() == 2:
            print(f"任务状态: {item.checkState()}")

    def add_item(self):
        row = self.table_model_.rowCount()
        self.table_model_.insertRow(row)

        comboBox = QComboBox()
        label_status = QLabel("None")
        button_remove = QPushButton("Delete")
        button_run = QPushButton("Run")

        button_remove.clicked.connect(lambda checked, r=row: self.remove_row(r))
        button_run.clicked.connect(lambda checked, r=row: self.run_point(r))

        self.table_view_.setIndexWidget(self.table_model_.index(row, 0), comboBox)
        self.table_view_.setIndexWidget(self.table_model_.index(row, 1), label_status)
        self.table_view_.setIndexWidget(self.table_model_.index(row, 2), button_remove)
        self.table_view_.setIndexWidget(self.table_model_.index(row, 3), button_run)

    def remove_row(self, row):
        self.table_model_.removeRow(row)

    def run_point(self, row):
        # 这里可以添加运行点位的逻辑
        print(f"Running point {row}")

    def save_point_chain(self):
        # 获取所有点位
        points = []
        for row in range(self.table_model_.rowCount()):
            point_name_item = self.table_model_.item(row, 0)
            if point_name_item is not None:
                point_name = point_name_item.text()
                points.append(point_name)
        
        # 这里可以添加保存点位链的逻辑
        print(f"Saving point chain: {points}")

# 使用示例
if __name__ == "__main__":
    app = QApplication(sys.argv)
    view = NavGoalTableView()
    view.show()
    sys.exit(app.exec_())